﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Windows.Forms;

public partial class qiantai_yuding : System.Web.UI.Page
{
    string roomNo = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       
        Application.Lock();
        roomNo = Application["name"].ToString();
        Application.UnLock();
        txtHNo.Text = roomNo;
    }

    protected void yuding_Click(object sender, EventArgs e)
    {
        DateTime InTime = Convert.ToDateTime(Calendar1.SelectedDate);
        DateTime OutTime = Convert.ToDateTime(Calendar2.SelectedDate);
        TimeSpan ts = OutTime - InTime;
        int d = Convert.ToInt32(ts.Days);
        string CName = txtCName.Text.Trim();
        string telephone = txtTelephone.Text.Trim();
        int totalPrice=0;

        string sql2 = string.Format("select NormalPrice from [Hotel] where Hotel.HNo='{0}'", roomNo);
        string connection = @"Data Source=.;Initial Catalog=HotelManagement;Integrated Security=True";


        SqlConnection conn;

        using (conn = new SqlConnection(connection))
        {
            SqlCommand cmd2 = new SqlCommand(sql2, conn);
            conn.Open();
            SqlDataReader dr = cmd2.ExecuteReader();
            if (dr.Read())
            {
                int normalPrice = Convert.ToInt32(dr["NormalPrice"]);
                totalPrice = d * normalPrice;
                //txtTotalPrice.Text = totalPrice.ToString();
            }

        }
        string sql = string.Format("insert into [Order](CName,HNo,InTime,OutTime,Telephone,TotalPrice,SNo) values( '{0}','{1}','{2}','{3}','{4}',{5},'1031')",
                CName, roomNo, InTime, OutTime, telephone, totalPrice);
        string sql1 = string.Format("update [Hotel] set HState=1  where Hotel.HNo='{0}'", roomNo);
        using (conn = new SqlConnection(connection))
        {

            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlCommand cmd1 = new SqlCommand(sql1, conn);
            conn.Open();

            int n = Convert.ToInt32(cmd.ExecuteNonQuery());
            int n1 = Convert.ToInt32(cmd1.ExecuteNonQuery());

            if (n != 1 && n1 != 1)
            {
                Response.Write("<script language=javascript>alert('预定失败！');</script>");
            }
            else
            {
                //Response.Write("<script language=javascript>alert('预订成功！\n总价格为:'+totalPrice);</script>");
                //Response.Redirect("Order1.aspx");
                MessageBox.Show("预订成功！\n总价格为:"+totalPrice);
                Response.Redirect("Order1.aspx");
            }

        }
    }
    
       
        
       
        
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtInTime.Text = Convert.ToString(Calendar1.SelectedDate);
        Calendar1.Visible = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Calendar2.Visible = true;
    }
    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        txtOutTime.Text = Convert.ToString(Calendar2.SelectedDate);
        Calendar2.Visible = false;
    }
    protected void txtTotalPrice_TextChanged(object sender, EventArgs e)
    {

    }
}